package com.zkh360.service.message.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.zkh360.service.message.constant.ErrorCode;
import com.zkh360.service.message.domain.SysRoles;
import com.zkh360.service.message.domain.SysUsersRoles;
import com.zkh360.service.message.exceptions.BusinessException;
import com.zkh360.service.message.mapper.RoleMapper;
import com.zkh360.service.message.mapper.UserRoleMapper;
import com.zkh360.service.message.mapper.UsersMapper;
import com.zkh360.service.message.model.Pager;
import com.zkh360.service.message.model.SysRoleVO;
import com.zkh360.service.message.model.SysUserVO;
import com.zkh360.service.message.model.SysUsersRolesVO;
import com.zkh360.service.message.service.RoleService;

import lombok.extern.slf4j.Slf4j;

/**
 * 角色serviceImpl
 * 
 * @author chuanxi.zhao
 *
 */
@Service(value = "RoleService")
@Slf4j
public class RoleServiceImpl implements RoleService {

	@Autowired
	private RoleMapper roleMapper;

	@Autowired
	private UsersMapper usersMapper;
	
	@Autowired
	private UserRoleMapper userRoleMapper;

	@Override
	public List<SysRoleVO> getRoles(SysRoleVO sysRoleVo, Pager pager) throws BusinessException {
		SysRoles sysRoles = new SysRoles();
		sysRoles.setRole_id(sysRoleVo.getRole_id());
		sysRoles.setRole_name(sysRoleVo.getRole_name());
		List<SysRoles> SysRoleList = roleMapper.selectByRowBounds(sysRoles,
				new RowBounds(pager.getStartRow(), pager.getPageSize()));
		List<SysRoleVO> result = new ArrayList<>();
		SysRoleList.forEach(sysRole -> {
			SysRoleVO sysRoleVoTemp = new SysRoleVO();
			BeanUtils.copyProperties(sysRole, sysRoleVoTemp);
			result.add(sysRoleVoTemp);
		});

		return result;
	}

	@Override
	public List<SysRoleVO> getRolesByUser(SysUserVO sysUsers, Pager pager) throws BusinessException {
		log.info("");
		List<SysRoles> SysRoleList = roleMapper.getRolesByUser(sysUsers.getUser_id(), sysUsers.getUser_name(),
				pager.getStartRow(), pager.getPageSize());
		List<SysRoleVO> result = new ArrayList<>();
		SysRoleList.forEach(sysRole -> {
			SysRoleVO sysRoleVoTemp = new SysRoleVO();
			BeanUtils.copyProperties(sysRole, sysRoleVoTemp);
			result.add(sysRoleVoTemp);
		});

		return result;
	}

	@Override
	public void editRole(SysRoleVO sysRoleVo) {
		SysRoles sysRolesParams = new SysRoles();
		sysRolesParams.setRole_id(sysRoleVo.getRole_id());
		SysRoles sysRolesDb = roleMapper.selectOne(sysRolesParams);

		sysRolesDb.setEnabled(sysRoleVo.getEnabled());
		sysRolesDb.setIssys(sysRoleVo.getIssys());
		sysRolesDb.setRole_desc(sysRoleVo.getRole_desc());
		sysRolesDb.setRole_name(sysRoleVo.getRole_name());
		roleMapper.updateByPrimaryKey(sysRolesDb);
	}

	@Override
	public void deleteRole(Long roleId) {
		List<Long> roleIdList = new ArrayList<>();
		roleIdList.add(roleId);
		// 需要判断该角色没有用户使用才可以删除
		List<SysUserVO> users = usersMapper.getUserByRoleId(roleIdList, 1);
		if (users.size() > 0) {
			throw BusinessException.customException(ErrorCode.CODE_FAILURE, "存在用户配置该角色，无法删除");
		} else {
			SysRoles sysRolesParams = new SysRoles();
			sysRolesParams.setRole_id(roleId);
			SysRoles sysRolesDb = roleMapper.selectOne(sysRolesParams);
			sysRolesDb.setEnabled(0);
			roleMapper.updateByPrimaryKey(sysRolesDb);
		}
	}

	@Override
	public void addRole(SysRoleVO sysRoleVo) {
		SysRoles sysRoles = new SysRoles();
		sysRoles.setEnabled(sysRoleVo.getEnabled());
		sysRoles.setIssys(sysRoleVo.getIssys());
		sysRoles.setRole_desc(sysRoleVo.getRole_desc());
		sysRoles.setRole_name(sysRoleVo.getRole_name());
		roleMapper.insert(sysRoles);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
	public void ediRoleByUser(List<SysUsersRolesVO> sysUsersRolesVoList) {
		// 清除原来的客户权限资源信息
		SysUsersRoles sysUsersRolesParams = new SysUsersRoles();
		sysUsersRolesParams.setUser_id(sysUsersRolesVoList.get(0).getUser_id());
		userRoleMapper.delete(sysUsersRolesParams);
		// 查询所有资源数据，方便后面找到父节点
		List<SysUsersRoles> sysUsersRolesList = new ArrayList<>();
		sysUsersRolesVoList.forEach(sysUsersRolesVo -> {
			SysUsersRoles temp = new SysUsersRoles();
			temp.setRole_id(sysUsersRolesVo.getRole_id());
			temp.setUser_id(sysUsersRolesVo.getUser_id());
			temp.setEnabled(1);
			sysUsersRolesList.add(temp);
		});
		userRoleMapper.insertList(sysUsersRolesList);
	}

	@Override
	public int getRolesCount(SysUserVO sysUsers) {
		return roleMapper.getRolesCount(sysUsers);
	}

}
